/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DatabaseManeger;

import java.io.File;
import java.net.URL;
import java.util.Arrays;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import me.aviator.entity.HibernateUtil;
import me.aviator.service.impl.BasemapService;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 * FXML Controller class
 *
 * @author 望舒凌霄
 */
public class MBaseMapController implements Initializable {
    @FXML
    private GridPane RootPane;
    @FXML
    private TextField MName;
    @FXML
    private TextField SVGPath;
    @FXML
    private Button BtnOpenSVG;
    @FXML
    private TextField ImagePath;
    @FXML
    private Button BtnOpenImage;
    @FXML
    private ChoiceBox<String> CBFeatureType;
    @FXML
    private ChoiceBox<String> CBMapProjection;
    @FXML
    private ChoiceBox<String> CBScale;
    @FXML
    private ChoiceBox<String> CBLevel;
    @FXML
    private ChoiceBox<String> CBArea;
    @FXML
    private Button BtnClose;
    @FXML
    private Button BtnAdd;
    
    private final BaseMapCString StringBox=new BaseMapCString();
    private String NewSVGName="";
    private File svgxml=null;
    private File svgImage=null;
    private String FeatureTypeString="全部";
    private String ProjectionString="双标准纬线等面积圆锥投影";
    private String ScaleString="全部";
    private String LevelString="全国";
    private String AreaString="中国";
    
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        //预添加要素分类
        String[] S=StringBox.getFeatureType();
        ObservableList<String> FeatureT=FXCollections.observableArrayList();
        FeatureT.addAll(Arrays.asList(S));
        FeatureT.remove(0);
        CBFeatureType.setItems(FeatureT);
        CBFeatureType.getSelectionModel().select(0);
        
        //预添加投影
        ObservableList<String> MapP=FXCollections.observableArrayList();
        S=StringBox.getMapProjection();
        MapP.addAll(Arrays.asList(S));
        CBMapProjection.setItems(MapP);
        CBMapProjection.getSelectionModel().select(0);
        //预添加比例尺
        ObservableList<String> ScaleS=FXCollections.observableArrayList();
        S=StringBox.getScale();
        ScaleS.addAll(Arrays.asList(S));
        ScaleS.remove(0);
        CBScale.setItems(ScaleS);
        CBScale.getSelectionModel().select(0);
        
        //预添加分级
        ObservableList<String> LevelS=FXCollections.observableArrayList();
        S=StringBox.getLevel();
        LevelS.addAll(Arrays.asList(S));
        CBLevel.setItems(LevelS);
        CBLevel.getSelectionModel().select(0);
        
        //预添加区域
        ObservableList<String> AreaS=FXCollections.observableArrayList();
        S=StringBox.getArea();
        AreaS.addAll(Arrays.asList(S));
        CBArea.setItems(AreaS);
        CBArea.getSelectionModel().select(0); 
    }    


    @FXML
    private void HandleBtnOpenSVG(ActionEvent event) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("导入SVG文件");
        fileChooser.setInitialDirectory(new File(System.getProperty("user.home"))); 
        fileChooser.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("SVG文件", "*.svg;*.SVG")
        );
        Stage s = new Stage(); 
        svgxml = fileChooser.showOpenDialog(s);
        if (svgxml != null) {
            String SVGPathString=svgxml.getPath();
            SVGPath.setText(SVGPathString);
        }
    }

    @FXML
    private void HandleBtnOpenImage(ActionEvent event) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("导入SVG例图文件");
        fileChooser.setInitialDirectory(new File(System.getProperty("user.home"))); 
        fileChooser.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("图片文件", "*.jpg;*.jpeg;*.png;*.gif;*.bmp;*.tif;*.tiff")
        );
        Stage s = new Stage(); 
        svgImage = fileChooser.showOpenDialog(s);
        if (svgImage != null) {
            String ImagePathString=svgImage.getPath();
            ImagePath.setText(ImagePathString);
        }
    }

    @FXML
    private void HandleBtnClose(ActionEvent event) {
        RootPane.getScene().getWindow().hide();
    }

    @FXML
    private void HandleBtnAdd(ActionEvent event) {
        NewSVGName=MName.getText();
        FeatureTypeString=CBFeatureType.getSelectionModel().getSelectedItem();
        ProjectionString=CBMapProjection.getSelectionModel().getSelectedItem();
        ScaleString=CBScale.getSelectionModel().getSelectedItem();
        LevelString=CBLevel.getSelectionModel().getSelectedItem();
        AreaString=CBArea.getSelectionModel().getSelectedItem();
        
        SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
        Session sess=sessionFactory.openSession();
        Transaction tx=sess.beginTransaction();
        
        BasemapService basemapService=new BasemapService(sess);
        basemapService.uploadBasemap(NewSVGName, FeatureTypeString, ProjectionString, ScaleString, LevelString, AreaString, svgxml, svgImage);
        
        tx.commit();
        sess.close();
        sessionFactory.close();
        
        RootPane.getScene().getWindow().hide();
    }
    
}
